<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
use app\common\model\Teacherreviews as TeacherreviewsModel;

/**
 * 老师接口
 */
class Teacherreviews extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new TeacherreviewsModel();
    }

    /**
     * 老师点评列表
     * @ApiTitle    (老师点评列表)
     * @ApiMethod  (GET)
     * @ApiParams   (name="student_id", type="int", required=true, description="学生ID")
     * @ApiParams   (name="homework_id", type="int", required=true, description="作业ID")
     * @ApiParams   (name="page", type="integer", required=false, description="页码")
     * @ApiParams   (name="limit", type="integer", required=false, description="每页数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
          'code':'200',
          'msg':'查询成功',
          'time': "",
          'data': {
        }
     */
    public function reviewsIndex(){

        $data = input('get.');
        $studentId = $data['student_id'];
        $homeworkId = $data['homework_id'];
        if (!$studentId || !Validate::is($studentId, 'number') || !$homeworkId || !Validate::is($homeworkId, 'number')) {
            $this->error('参数错误');
        }

        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;
        
        $dis = [
            'homework_id' => $homeworkId,
            'student_id' => $studentId,
        ];
        $row = $this->model->dataList($dis, 'id desc', $page, $limit);
        $this->success('查询成功', $row);
    }

    /**
     * 新增老师点评
     * @ApiTitle    (新增老师点评)
     * @ApiMethod  (POST)
     * @ApiParams   (name="teacher_id", type="int", required=true, description="老师ID")
     * @ApiParams   (name="homework_id", type="int", required=true, description="作业ID")
     * @ApiParams   (name="student_id", type="int", required=true, description="学生ID")
     * @ApiParams   (name="content", type="string", required=true, description="内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="新增成功")
     * @ApiReturn   ({
          'code':'200',
          'msg':'新增成功',
          'time': "",
          'data': {
        }
     */
    public function reviewsAdd(){
        $data = input('post.');
        $teacherId = $data['teacher_id'];
        $homeworkId = $data['homework_id'];
        $studentId = $data['student_id'];
        $content = $data['content'];
        if (!$teacherId || !Validate::is($teacherId, 'number')) {
            $this->error('老师ID不能为空');
        }
        if (!$homeworkId || !Validate::is($homeworkId, 'number')) {
            $this->error('作业ID不能为空');
        }
        if (!$content) {
            $this->error('内容不能为空');
        }
        if (!$studentId || !Validate::is($studentId, 'number')) {
            $this->error('学生ID不能为空'); 
        }

        $this->model->dataAdd($data);
        $this->success('新增成功', $data);
    }
}
